package com.ple.app.suggest;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.ple.app.importdata.MyopenHelper;
import com.ple.app.jzsf.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SeedActivity extends Activity {

    private String name;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seed);

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        String[] conlm=new String[]{"id","name","tkw","seedrate","crop"};
        MyopenHelper myopenhelper=new MyopenHelper(SeedActivity.this,"seed.db",null,1);
        SQLiteDatabase db=myopenhelper.getReadableDatabase();
        Cursor curor=db.query("seed",conlm,null,null,null,null,null,null);
        while(curor.moveToNext()){
            Map<String, Object> item = new HashMap<String, Object>();
            String id=curor.getString(0);
            String name=curor.getString(1);
            String other1=curor.getString(2);
            String other2=curor.getString(3);
            item.put("id", id);
            item.put("name", name);
            item.put("tkw", other1);
            item.put("seedrate", other2);
            list.add(item);}
        curor.close();
        db.close();
        SimpleAdapter adapter = new SimpleAdapter(SeedActivity.this, list, R.layout.seed_item, new String[]{"id", "name", "tkw", "seedrate"},
                new int[]{R.id.ID, R.id.Name, R.id.other1, R.id.other2});
        listView = (ListView) findViewById(R.id.seed_info); //(ListView) findViewById(R.id.seed_datagrid);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap<String,String> getmap= (HashMap<String, String>) listView.getItemAtPosition(position);
                name=getmap.get("name");

                Intent intent=new Intent();
                    Bundle bundle=new Bundle();
                    bundle.putString("name",name);
                    intent.putExtras(bundle);
                    setResult(0,intent);
                    finish();
                // Toast.makeText(getApplicationContext(),name,Toast.LENGTH_LONG).show();
            }
        });
        /**
         *

        ok= (Button) findViewById(R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Intent intent = new Intent(seedinformation.this,SuggestActivity.class);
                //startActivity(intent);
                if (name==null){Toast.makeText(getApplicationContext(),"请选择种子信息！", Toast.LENGTH_LONG).show();}
                else
                {Intent intent=new Intent();
                Bundle bundle=new Bundle();
                bundle.putString("name",name);
                 intent.putExtras(bundle);
                setResult(0,intent);
                finish();}
            }
        });
        no= (Button) findViewById(R.id.no);
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });*/






    }

}
